<template>
  <base-load-page
    :loadState="pageLoad.loadState"
    :description="pageLoad.description"
    :pageLoadButton="[{ text: '重新加载', callBack: () => getBaseDataAxios() }]"
  >
    <template #head>
      <div style="padding-bottom: 15px" class="head-box">
        <div class="left-box"></div>
        <div class="right-box">
          <el-button type="success" :icon="IconSolarAddCircleLinear" @click="handleSubmit()">保存</el-button>
        </div>
      </div>
    </template>
    <el-form ref="elFormRef" :model="formData" :rules="rules" scrollToError statusIcon>
      <el-form-item label="抖音客服账号" prop="dySupportStaff" labelWidth="10em">
        <el-input v-model="formData.dySupportStaff" :maxlength="200" placeholder="请输入抖音客服账号" clearable></el-input>
      </el-form-item>
      <el-form-item label="客服电话" prop="appCustomorTel" labelWidth="10em">
        <el-input v-model="formData.appCustomorTel" :maxlength="200" placeholder="请输入客服电话" clearable></el-input>
      </el-form-item>
      <el-form-item label="K币兑换比例" prop="priceVirtualRate" labelWidth="10em">
        <el-input v-model="formData.priceVirtualRate" :maxlength="200" disabled clearable></el-input>
      </el-form-item>
    </el-form>
  </base-load-page>
</template>

<script setup lang="ts">
  import IconSolarAddCircleLinear from '~icons/solar/add-circle-linear';
  import type { FormInstance, FormRules } from 'element-plus';
  import { type AppBaseDict, editAppBaseDictAxios, getAppBaseDictAxios } from '@/api/app-management';

  onMounted(() => {
    getBaseDataAxios();
  });

  const pageLoad = shallowReactive<$PageLoad>({ loadState: true, description: '' });
  const elFormRef: Ref<FormInstance | null> = ref(null);
  const formData = ref<AppBaseDict>({
    appCustomorTel: '',
    dySupportStaff: '',
    priceVirtualRate: '',
  });
  const rules = computed<FormRules>(() => {
    const _rules: FormRules = {
      dySupportStaff: [{ required: true, type: 'string', message: '抖音客服账号不能为空', trigger: ['blur', 'change'] }],
      appCustomorTel: [{ required: true, type: 'string', message: '客服电话不能为空', trigger: ['blur', 'change'] }],
    };

    return _rules;
  });

  /**
   * 得到基础数据
   */
  async function getBaseDataAxios() {
    try {
      pageLoad.loadState = true;
      const res = await getAppBaseDictAxios();
      formData.value = {
        appCustomorTel: res.data.appCustomorTel || '',
        dySupportStaff: res.data.dySupportStaff || '',
        priceVirtualRate: res.data.priceVirtualRate || '',
      };
      pageLoad.loadState = false;
    } catch (err) {
      pageLoad.loadState = 'err';
      pageLoad.description = String((err as Record<string, unknown>).msg || '') || '获取数据失败';
    }
  }

  /** 提交 */
  async function handleSubmit() {
    try {
      await elFormRef.value?.validate();
      try {
        await editAppBaseDictAxios({ appCustomorTel: formData.value.appCustomorTel, dySupportStaff: formData.value.dySupportStaff });
        ElToast.success('提交成功');
      } catch (error) {
        ElToast.error(error.msg || '提交失败，-BD003');
      }
    } catch (error) {
      ElToast.error('提交失败，请完善表单，-BD002');
    }
  }
</script>

<style lang="scss" scoped>
  .pagination-box {
    @include flex(row, nowrap, center, center, center);
    padding-top: 15px;
  }

  .head-box {
    @include flex(row, nowrap, flex-start, center, center);

    .left-box {
      display: grid;
      flex: 1;
      grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
      grid-gap: 15px;
      margin-right: 15px;
    }
  }
</style>
